top_builddir = ../../../..

include ../../../../config.mk

.PHONY: all clean rebuild install uninstall

PERLCCFLAGS = $(DESTDIR)$(shell eval "`$(PERL) -V:ccflags`"; export ccflags; echo "$$ccflags " | sed -e 's| -belf||g' -e 's|-KPIC||g' -e 's|-x.* | |g' -e "s|UNKNOWN||g" -e "s|-Dbool=char||g" -e "s|-mtune=.* | |g")
PERLOPTIMIZE = $(DESTDIR)$(shell eval "`$(PERL) -V:optimize`"; export optimize; echo "$$optimize " | sed -e 's| -belf||g' -e 's|-KPIC||g' -e 's|-x.* | |g' -e "s|UNKNOWN||g" -e "s|-Dbool=char||g" -e "s|-mtune=.* | |g")
ifeq ($(OVERRIDEPERLSITEARCH),)
PERLSITEARCH = $(DESTDIR)$(shell eval "`$(PERL) -V:sitearch`"; export sitearch; echo $$sitearch | sed -e "s|UNKNOWN||g" )
else
PERLSITEARCH = $(DESTDIR)$(OVERRIDEPERLSITEARCH)
endif
PERL_INC = $(DESTDIR)$(shell eval "`$(PERL) -V:archlibexp`"; export archlibexp; echo "-I$$archlibexp/CORE" | sed -e "s|UNKNOWN||g" )
PERL_LIB = $(DESTDIR)$(shell eval "`$(PERL) -V:privlibexp`"; export privlibexp; echo $$privlibexp | sed -e "s|UNKNOWN||g" )
ifeq ($(OVERRIDEPERLINSTALLMAN3DIR),)
PERLINSTALLMAN3DIR = $(DESTDIR)$(shell eval "`$(PERL) -V:installman3dir`"; export installman3dir; echo $$installman3dir | sed -e "s|UNKNOWN||g" )
else
PERLINSTALLMAN3DIR = $(DESTDIR)$(OVERRIDEPERLINSTALLMAN3DIR)
endif
ifeq ($(OVERRIDEPERLMAN3EXT),)
PERLMAN3EXT = $(shell eval "`$(PERL) -V:man3ext`"; export man3ext; echo $$man3ext | sed -e "s|UNKNOWN||g" )
else
PERLMAN3EXT = $(OVERRIDEPERLMAN3EXT)
endif

CPPFLAGS = $(BASECPPFLAGS) $(PERLOPTIMIZE) $(PERLCCFLAGS) -I./ -I$(top_builddir) -I$(top_builddir)/src/api/c++/include $(RUDIMENTSINCLUDES) $(PERL_INC)
LIBS = $(PERLLIB) -L$(top_builddir)/src/api/c++/src -lsqlrclient $(RUDIMENTSLIBS) $(LIBDMALLOC) $(LIBEFENCE)
RPATH =
ifneq ($(strip $(libdir)),)
ifeq ($(PERLUSERPATH),yes)
	RPATH = -R $(libdir)
endif
endif

.SUFFIXES: .lo .xs

.xs.C:
	$(PERL) $(PERL_LIB)/ExtUtils/xsubpp -noprototypes -C++ -typemap $(PERL_LIB)/ExtUtils/typemap -typemap perlobject.map -typemap typemap $< > $<c && mv $<c $@

.C.lo:
	cp perlincludes1.h perlincludes.h; \
	$(LIBTOOL) --mode=compile $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@ 2> errors1; \
	if ( test ! -r Cursor.lo ); then \
		cp perlincludes2.h perlincludes.h; \
		$(LIBTOOL) --mode=compile $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@ 2> errors2; \
		if ( test ! -r Cursor.lo ); then \
			cp perlincludes3.h perlincludes.h; \
			$(LIBTOOL) --mode=compile $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@ 2> errors3; \
			if ( test ! -r Cursor.lo ); then \
				cp perlincludes4.h perlincludes.h; \
				$(LIBTOOL) --mode=compile $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@ 2> errors4; \
				if ( test ! -r Cursor.lo ); then \
					if ( test -s errors1 ); then \
						echo "first try errors : "; cat errors1; echo; \
					fi; \
					if ( test -s errors2 ); then \
						echo "second try errors : "; cat errors2; echo; \
					fi; \
					if ( test -s errors3 ); then \
						echo "third try errors : "; cat errors3; echo; \
					fi; \
					if ( test -s errors4 ); then \
						echo "third try errors : "; cat errors4; echo; \
					fi; \
					exit 1; \
				else \
					cat errors4; \
				fi; \
			else \
				cat errors3; \
			fi; \
		else \
			cat errors2; \
		fi; \
	else \
		cat errors1; \
	fi; \
	$(RM) errors1 errors2 errors3 errors4 perlincludes.h


all: libCursor.la

clean:
	$(LIBTOOL) --mode=clean $(RM) *.C
	$(LIBTOOL) --mode=clean $(RM) *.la
	$(LIBTOOL) --mode=clean $(RM) *.lo*
	$(LIBTOOL) --mode=clean $(RM) *.o	
	$(LIBTOOL) --mode=clean $(RM) *.so
	$(LIBTOOL) --mode=clean $(RM) *.$(PERLMAN3EXT)
	$(LIBTOOL) --mode=clean $(RM) errors*
	$(LIBTOOL) --mode=clean $(RM) perlincludes.h
	$(RMTREE) .libs

rebuild: clean all

libCursor.la: Cursor.xs Cursor.C Cursor.lo
	$(LIBTOOL) --mode=link $(CXX) -o $@ Cursor.lo $(LDFLAGS) $(LIBS) -rpath $(PERLSITEARCH)/auto/SQLRelay/Cursor $(RPATH) -module -no-undefined

Cursor.so:
	if ( test -r .libs/libCursor.0.0.0 ); \
	then \
		cp .libs/libCursor.0.0.0 Cursor.so; \
	fi
	if ( test -r .libs/libCursor.0.0 ); \
	then \
		cp .libs/libCursor.0.0 Cursor.so; \
	fi
	if ( test -r .libs/libCursor.0 ); \
	then \
		cp .libs/libCursor.0 Cursor.so; \
	fi
	if ( test -r .libs/libCursor ); \
	then \
		cp .libs/libCursor Cursor.so; \
	fi
	if ( test -r .libs/libCursor.so.0.0.0 ); \
	then \
		cp .libs/libCursor.so.0.0.0 Cursor.so; \
	fi
	if ( test -r .libs/libCursor.so.0.0 ); \
	then \
		cp .libs/libCursor.so.0.0 Cursor.so; \
	fi
	if ( test -r .libs/libCursor.so.0 ); \
	then \
		cp .libs/libCursor.so.0 Cursor.so; \
	fi
	if ( test -r .libs/libCursor.so ); \
	then \
		cp .libs/libCursor.so Cursor.so; \
	fi

Cursor.dll:
	if ( test -r .libs/cygCursor-0.dll ); \
	then \
		cp .libs/cygCursor-0.dll Cursor.dll; \
	fi

ifeq ($(CYGWIN),)
install: Cursor.so
	$(MKINSTALLDIRS) $(PERLSITEARCH)/SQLRelay
	$(INSTALL) -m 644 Cursor.pm $(PERLSITEARCH)/SQLRelay
	$(MKINSTALLDIRS) $(PERLSITEARCH)/auto/SQLRelay/Cursor
	$(LTINSTALL) -m 755 Cursor.so $(PERLSITEARCH)/auto/SQLRelay/Cursor
	touch $(PERLSITEARCH)/auto/SQLRelay/Cursor/Cursor.bs
	$(RM) Cursor.so
ifneq ($(POD2MAN),)
ifneq ($(PERLINSTALLMAN3DIR),)
	$(POD2MAN) Cursor.pm > SQLRelay::Cursor.$(PERLMAN3EXT)
	$(MKINSTALLDIRS) $(PERLINSTALLMAN3DIR)
	$(INSTALL) -m 644 SQLRelay::Cursor.$(PERLMAN3EXT) $(PERLINSTALLMAN3DIR)
	$(RM) SQLRelay::Cursor.$(PERLMAN3EXT)
	echo $(PERLINSTALLMAN3DIR)/SQLRelay::Cursor.$(PERLMAN3EXT) > $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist
endif
endif
	echo $(PERLSITEARCH)/auto/SQLRelay/Cursor/Cursor.so > $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist
	echo $(PERLSITEARCH)/auto/SQLRelay/Cursor/Cursor.bs > $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist
else
install: Cursor.dll
	$(MKINSTALLDIRS) $(PERLSITEARCH)/SQLRelay
	$(INSTALL) -m 644 Cursor.pm $(PERLSITEARCH)/SQLRelay
	$(MKINSTALLDIRS) $(PERLSITEARCH)/auto/SQLRelay/Cursor
	$(LTINSTALL) -m 755 Cursor.dll $(PERLSITEARCH)/auto/SQLRelay/Cursor
	touch $(PERLSITEARCH)/auto/SQLRelay/Cursor/Cursor.bs
	$(RM) Cursor.dll
	echo $(PERLSITEARCH)/auto/SQLRelay/Cursor/Cursor.dll > $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist
	echo $(PERLSITEARCH)/auto/SQLRelay/Cursor/Cursor.bs > $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist
endif

uninstall:
	$(RM) `cat $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist`
	$(RM) $(PERLSITEARCH)/auto/SQLRelay/Cursor/.packlist
